﻿using System;
using System.Collections.Generic;
using System.Linq;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Architecture;
using YArchitech.Revit;

namespace YJKRevitStairs.StairsModel.CommonUnit
{
	public class StairsOperationInterface
	{
		public static XYZ GetStairsBtmRunPtOnXYPlane(Autodesk.Revit.DB.Document rvtDoc, ElementId stairsId)
		{
			XYZ xyz = null;
			Stairs stairs = rvtDoc.GetElement(stairsId) as Stairs;
			if (stairs != null)
			{
				ICollection<ElementId> stairsRuns = stairs.GetStairsRuns();
				if (stairsRuns.Count > 0)
				{
					ElementId elementId = stairsRuns.First<ElementId>();
					StairsRun stairsRun = rvtDoc.GetElement(elementId) as StairsRun;
					if (stairsRun != null)
					{
						CurveLoop footprintBoundary = stairsRun.GetFootprintBoundary();
						List<Curve> list = new List<Curve>();
						foreach (Curve curve in footprintBoundary)
						{
							if (null != curve)
							{
								list.Add(curve);
							}
						}
						foreach (Curve curve2 in list)
						{
							XYZ endPoint = curve2.GetEndPoint(1);
							xyz = new XYZ(endPoint.X, endPoint.Y, 0.0);
							if (xyz != null)
							{
								break;
							}
						}
					}
				}
			}
			return xyz;
		}

		private static Level GetStairsBtmLevel(Element ele)
		{
			Level result = null;
			if (ele != null)
			{
				ElementId elementId = ele.GetParameter(BuiltInParameter.STAIRS_BASE_LEVEL_PARAM).AsElementId();
				if (null != elementId)
				{
					result = (ele.Document.GetElement(elementId) as Level);
				}
			}
			return result;
		}
	}
}
